﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProxyPattern
{
    /// <summary>
    /// 代理的火车站
    /// </summary>
    public class ProxySubject : ISubject
    {
        public ProxySubject()
        {
            //Thread.Sleep(2000);
            Console.WriteLine("{0}被构造", this.GetType().Name);
        }

        private static ISubject _subject = null;

        private ISubject _Subject
        {
            get
            {
                //多线程得加锁
                if (_subject == null)
                {
                    _subject = new RealSubject();
                }
                return _subject;
            }
        }

        /// <summary>
        /// 查询余票
        /// </summary>
        public void DoSomething()
        {
            try
            {
                //权限检测  单例代理
                Console.WriteLine("Log:DoSomething start");
                _Subject.DoSomething();
                Console.WriteLine("Log:DoSomething end");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 买票
        /// </summary>
        public void GetSomething()
        {
            Console.WriteLine("Log:GetSomething start");
            _Subject.GetSomething();
            Console.WriteLine("Log:GetSomething end");
        }

    }
}
